#include "LevelManager.h"

int LevelManager::nextID = 0;
int LevelManager::currentID = 0;
LevelManager* LevelManager::instance = 0;
std::hash_map<int, Level*> LevelManager::levels = std::hash_map<int, Level*>();

LevelManager::~LevelManager()
{
	auto iter = levels.begin();
	for (; iter != levels.end(); iter++)
	{
		delete iter->second;
		iter->second = 0;
	}
	levels.clear();
}

void LevelManager::init()
{
	LevelManager::instance = new LevelManager();
}

void LevelManager::draw(ID3DXSprite *sprite)
{
	Level* level = LevelManager::getCurrentLevel();
	level->draw(sprite);
}

void LevelManager::registerLevel(Level *level)
{
	if (level->getID() == -1)
	{
		while (levels.find(LevelManager::nextID) != levels.end())
			++LevelManager::nextID;

		level->setID(nextID);
	}
	else if (levels.find(level->getID()) != levels.end())
		throw; // Level of ID exists. Throw exception

	levels[level->getID()] = level;
}

void LevelManager::removeLevel(Level *level)
{
	auto iter = levels.begin();
	if (iter != levels.end())
		levels.erase(iter);
}

Level* LevelManager::getLevel(int id)
{
	auto iter = levels.find(id);
	if (iter != levels.end())
		return iter->second;
	return 0;
}

/**
 * Return a true or false dependant on if the level exists.
 */
bool LevelManager::levelExists(int id)
{
	auto iter = levels.find(id);
	if (iter != levels.end())
		return true;
	else
		return false;
}